package ${domain}.action.ajax.wsys;

import com.alibaba.excel.support.ExcelTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;
import ${domain}.frame.auth.LocalData;
import ${domain}.frame.base.BaseResponse;
import ${domain}.frame.base.ErrorType;
import ${domain}.frame.excel.WExcel;
import ${domain}.frame.excel.exception.TemplateNotMatchException;
import ${domain}.frame.utils.LogUtil;
import ${domain}.frame.utils.MapperUtil;
import ${domain}.frame.utils.ResponseUtil;
import ${domain}.frame.utils.ValidationUtil;
import ${domain}.module.wsys.ent.DictItem;
import ${domain}.module.wsys.mgr.DictItemManager;
import ${domain}.module.wsys.req.DictItemCreateRequest;
import ${domain}.module.wsys.req.DictItemDeleteRequest;
import ${domain}.module.wsys.req.DictItemFindRequest;
import ${domain}.module.wsys.req.DictItemUpdateRequest;
import ${domain}.module.wsys.rsp.DictItemCreateResponse;
import ${domain}.module.wsys.rsp.DictItemDeleteResponse;
import ${domain}.module.wsys.rsp.DictItemFindResponse;
import ${domain}.module.wsys.rsp.DictItemUpdateResponse;

import java.io.IOException;
import java.util.List;

public class DictItemAjax {

    @Autowired
    private DictItemManager dictItemManager;

    public DictItemCreateResponse create(DictItemCreateRequest request) {
        return dictItemManager.create(request, LocalData.getToken());
    }

    public DictItemDeleteResponse delete(DictItemDeleteRequest request) {
        return dictItemManager.delete(request, LocalData.getToken());
    }

    public DictItemUpdateResponse update(DictItemUpdateRequest request) {
        return dictItemManager.update(request, LocalData.getToken());
    }

    public DictItemFindResponse find(DictItemFindRequest request) {
        return dictItemManager.find(request, LocalData.getToken());
    }

    public Object template() {
        return ResponseUtil.apply(new WExcel<>(DictItem.class));
    }

    public Object exports(DictItemFindRequest request) {
        DictItemFindResponse response = dictItemManager.find(request, LocalData.getToken());
        if (response.hasError()) {
            return response;
        } else if (response.getTotalCount() == 0) {
            response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空");
            return response;
        }
        return ResponseUtil.apply(new WExcel<DictItem>(DictItem.class).addDatas(response.getResult()));
    }

    public Object imports(MultipartFile file) {
        BaseResponse baseResponse = new BaseResponse();
        try {
            // 检查文件格式
            String originalFilename = file.getOriginalFilename() != null ? file.getOriginalFilename() : "";
            if (originalFilename.matches("\\.xlsx?")) {
                baseResponse.addError(ErrorType.BUSINESS_ERROR, "上传文件格式错误！");
                return baseResponse;
            }
            // 兼容2003以前老版本.xls
            ExcelTypeEnum excelTypeEnum = file.getOriginalFilename().endsWith(".xlsx") ? ExcelTypeEnum.XLSX : ExcelTypeEnum.XLS;
            WExcel sheet = new WExcel<>(DictItem.class).read(file.getBytes(), excelTypeEnum, new WExcel.Processor<DictItem>() {
                @Override
                public List<String> exec(DictItem o, int index) {
                    DictItemCreateRequest request = MapperUtil.map(o, DictItemCreateRequest.class);
                    return ValidationUtil.validate(request);
                }
            });

            if (sheet.hasError()) {
                return ResponseUtil.apply(sheet.getBytes(), sheet.getName() + "-检查.xlsx");
            } else {
                return baseResponse;
            }
        } catch (IOException e) {
            e.printStackTrace();
            LogUtil.dumpException(e);
            baseResponse.addError(ErrorType.BUSINESS_ERROR, "上传文件出错");
        } catch (TemplateNotMatchException e) {
            baseResponse.addError(ErrorType.BUSINESS_ERROR, e.getMessage());
        }
        return baseResponse;
    }
}
